package org.jackysoft.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;
import java.util.UUID;

import javax.annotation.Resource;

import org.jackysoft.entity.Attachment;
import org.jackysoft.entity.HibernateWorkflowEntry;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.AttachmentService;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.util.AttachmentType;
import org.jackysoft.util.FileInfo;
import org.jackysoft.util.IOUtils;
import org.jackysoft.util.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

@Service
@Transactional
public class AttachmentServiceImpl extends
		AbstractStringIdServiceImpl<Attachment> implements AttachmentService {
	protected WorkflowService workflowService;
	/**
	 * 
	 */
	private static final long serialVersionUID = -4238828916766329911L;

	public AttachmentServiceImpl() {
		this.type = Attachment.class;
	}

	@Override
	public void remove(Attachment bean, Long workflowId)
			throws DataAccessException {
		Attachment attachment = get(bean.getId());
		HibernateWorkflowEntry entry = (HibernateWorkflowEntry) workflowService
				.findEntry(workflowId);
		entry.getAttachment().remove(attachment);

	}

	@Override
	public FileInfo download(String id) throws Exception {
		Attachment at = get(id);
		FileInfo di = new FileInfo();
		di.setTitle(StringUtils.toUtf8String(at.getName()));
		byte[] contents = IOUtils.toByteArray(new File(at.getPath()));
		di.setData(contents);
		di.setFileType(at.getExt());
		return di;
	}

	@Override
	public void create(File[] uploads, String[] names, long workflowId)
			throws DataAccessException, FileNotFoundException, IOException {
		final HibernateWorkflowEntry entry = (HibernateWorkflowEntry) workflowService
				.findEntry(workflowId);
		if (entry == null)
			throw new NullPointerException(String.format(
					"workflow entity with gaven id %d not found", workflowId));
		String base = System.getProperty("appBase");
		for (int i = 0; i < uploads.length; i++) {
			entry.getAttachment().add(save(uploads[i], names[i], base));
		}
	}

	@Override
	public Collection<Attachment> upload(File[] uploads, String[] names)
			throws DataAccessException {
		String base = System.getProperty("appBase");
		Collection<Attachment> aths = Lists.newArrayList();
		for (int i = 0; i < uploads.length; i++) {
			aths.add(save(uploads[i], names[i], base));

		}
		return aths;
	}

	@Override
	public Attachment save(File upload, String name, String base)
			throws DataAccessException {
		UUID uuid = UUID.randomUUID();
		String ext = StringUtils.splitPostFileName(name);
		String relatePath = "attachment" + File.separator + uuid.toString()
				+ "." + ext;
		String urlpath = "attachment/" + uuid.toString() + "." + ext;
		String fname = base + File.separator + relatePath;
		IOUtils.saveFile(upload, new File(fname));
		Attachment ath = new Attachment();
		ath.setUrlPath(urlpath);
		ath.setPath(fname);
		ath.setUploadTime(Calendar.getInstance());
		ath.setName(name);
		ath.setExt(ext);
		ath.setType(AttachmentType.getType(ext));
		return ath;
	}

	@Override
	public Attachment upload(File uploads, String names)
			throws DataAccessException {
		String base = System.getProperty("appBase");
		Attachment ath = save(uploads, names, base);
		super.create(ath);
		return ath;
	}

	@Resource
	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}

}
